package com.shilan.dllo.wangyi.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.shilan.dllo.wangyi.R;
import com.shilan.dllo.wangyi.adpter.Headline_ad_Adpter;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/28.
 */
public class Headline_ad_fragment extends Fragment{

    private ImageView adIv;//轮播图片
    private TextView titleTv;// 广告文字
    private  String title;
    private  String imgSrc;//图片地址
    private RequestQueue queue;//定义请求队列
    private Headline_ad_Adpter adpter;

    //单列模式
    private  static Headline_ad_fragment adFragment;

    public Headline_ad_fragment(){

    }

    public  static Headline_ad_fragment getInstance(String title,String imgSrc){
        Headline_ad_fragment adFragment = new Headline_ad_fragment();

        //传递参数到fragment
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("imgSrc", imgSrc);
        adFragment.setArguments(args);

        return  adFragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取参数
        Bundle bundle = getArguments();
        if (bundle != null){
            title = bundle.getString("title");
            imgSrc = bundle.getString("imgSrc");
        }
    }

    /**
     * 加载布局
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.headline_ad,null);

        return v;
    }

    /***
     * 初始化控件
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化
        adIv = (ImageView) view.findViewById(R.id.headAdIv);
        titleTv = (TextView) view.findViewById(R.id.headTitleTv);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        queue = Volley.newRequestQueue(getContext());
        adpter = new Headline_ad_Adpter(getFragmentManager());
        titleTv.setText(title);
        getBitmap(imgSrc, adIv);
    }

    public  void getBitmap(String url,final ImageView imageView){
        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                     //将返回的bitmap对象设置到图片组件中
                        imageView.setImageBitmap(response);
                    }
                },
                720,200, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener(){


                    @Override
                    public void onErrorResponse(VolleyError error) {

                        error.printStackTrace();
                    }
                }){
            //添加请求头 需要自己打getH
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> map =new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;
            }
        };
        //将本次图片请求添加到请求队列中
        queue.add(request);
    }
}
